package 二分查找;

public class 在排序数组中查找数字 {

    // 太low了，使用二分搜索
//    public int search(int[] nums, int target) {
//        int num=0;
//        for (int i = 0; i < nums.length; i++) {
//            if (nums[i]==target)
//                num++;
//        }
//        return num;
//    }
    public int search(int[] nums, int target) {
        int left =0,right = nums.length-1;
        int count = 0;
        while(left<right){
            int mid = (left+right)/2;
            if(nums[mid]>=target)
                right=mid;
            if(nums[mid]<target)
                left = mid+1;
        }
        while(left<nums.length&&nums[left++]==target)
            count++;
        return count;
    }
}
